import store from "./store";
import router from "./router";
import { getToken } from "./composables/auth";
import { InfoToast } from "./composables/toastUtils";
import { showProgress, hideProgress } from "./composables/toastUtils";
// 全局前置首位
router.beforeEach(async (to, from, next) => {
  console.log("守卫");
  //显示loading
  showProgress();

  const token = getToken();
  // 无权限返回
  if (!token && to.path !== "/login") {
    InfoToast("无用户信息，返回登录页面", "error");
    return next({ path: "/login" });
  }

  // 放置重复登陆
  if (token && to.path === "/login") {
    InfoToast("用户已登陆", "warning");
    return next({ path: "/index" });
  }

  //存储用户信息
  if (token) {
    await store.dispatch("getInfo");
  }

  // 动态显示网页标签
  let title = (to.meta.title ? to.meta.title : "") + "-IIMS"
  document.title = title
  next();
});

//全局后置首位
router.afterEach((to, from) => {
  hideProgress()
});
